/***********************************
 * @author David Buezas (david.buezas at gmail.com) Feel free to copy, modify and use the source as it fits you.
 * @modified Aksenov 14\06
 */

package ganga.custodian.plot;

import ganga.custodian.R;
import ganga.custodian.plot.SeekBarWithTwoThumb.SeekBarChangeListener;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.XYPlot;

public class PlotActivity extends Activity implements SeekBarChangeListener, OnTouchListener
    {
    private ToggleButton bpmButton;
    private ToggleButton kigButton;
    double inputData[]={600, 630, 660, 650, 610, 600, 610, 620, 610, 620, 630, 600, 630, 660, 650, 610, 600, 610, 620, 610, 620,
            630, 600, 630, 660, 650, 610, 600, 610, 620, 610, 620, 630, 600, 630, 660, 650, 610, 600, 610, 620, 610,
            620, 630, 600, 630, 660, 650, 610, 600, 610, 620, 610, 620, 630, 600, 630, 660, 650, 610, 600, 610, 620,
            610, 620, 630};
    HeartPlotData data = new HeartPlotData(inputData);
	private SeekBarWithTwoThumb seekBar;
    private  TextView textBegin, textEnd;

    public void bpmButton_Click(View v){
        if (data.selectSeries == 1){
            data.switchSeries();
            kigButton.setChecked(false);
        }
        else{
            bpmButton.setChecked(true);
            kigButton.setChecked(false);
        }
    }

    public void kigButton_Click(View v){
        if (data.selectSeries == 0){
            data.switchSeries();
            bpmButton.setChecked(false);
        }
        else{
            bpmButton.setChecked(false);
            kigButton.setChecked(true);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plotactivity);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        bpmButton = (ToggleButton)findViewById(R.id.toggleButton1);
        kigButton = (ToggleButton)findViewById(R.id.toggleButton2);
        data.heartPlot = (XYPlot)findViewById(R.id.mySimpleXYPlot);

        //PlotActivity слушает нажатия на heartPlot
        data.heartPlot.setOnTouchListener(this);
        data.buildXYPlot();
            
        data.switchSeries();
        textBegin = (TextView)findViewById(R.id.textView1);
        textEnd = (TextView)findViewById(R.id.textView22);
        seekBar = (SeekBarWithTwoThumb)findViewById(R.id.myseekbar);
        seekBar.setSeekBarChangeListener(this);
    }//onCreate

    public void SeekBarValueChanged(int Thumb1Value, int Thumb2Value){
    	textBegin.setText(getString(R.string.Begin) + ":" + Thumb1Value + " %");
    	textEnd.setText(getString(R.string.End) + ":" + Thumb2Value + " %");
        setDomainScaleValues(Thumb1Value, Thumb2Value);
        }

    private void setDomainScaleValues(int first, int second){
        int max;
        int min;

		if (first > second) {
			max = first;
			min = second;
		}
		else {
			max = second;
			min = first;
		}
        data.minY = Math.round((data.bmpSeries.getMaxX() / 100 * min) / 100) * 100;
        data.maxY = Math.round((data.bmpSeries.getMaxX() / 100 * max) / 100) * 100;
        data.MAXY = data.maxY;
        data.MINY = data.minY;
        checkBoundaries();
    }

    public void setScaleButton_Click(View v){
        }

    private PointF firstFinger;
    private float lastScrolling;
    private float lastZooming;

    public boolean onTouch(View arg0, MotionEvent event){
        switch (event.getAction() & MotionEvent.ACTION_MASK){
            case MotionEvent.ACTION_DOWN: // Start gesture
                firstFinger = new PointF(event.getX(), event.getY());

            case MotionEvent.ACTION_MOVE:
            {
                final PointF oldFirstFinger = firstFinger;
                firstFinger = new PointF(event.getX(), event.getY());
                lastScrolling = oldFirstFinger.x - firstFinger.x;
                lastZooming = (firstFinger.y - oldFirstFinger.y);
                zoom(lastZooming);
                scroll(lastScrolling);
                checkBoundaries();
                data.heartPlot.redraw();
                }
            }

        return true;
        }
    private double shiftOnScale = 100;

    private void zoom(float scale){
        shiftOnScale = (Math.round((data.maxY - data.minY) / 10000) * 100);

        if (scale > 10){
            if (data.minY > data.MINY)
                data.minY -= shiftOnScale;

            if (data.maxY < data.MAXY)
                data.maxY += shiftOnScale;
        }

        if (scale < -10 && (data.maxY - data.minY > (Math.round(data.bmpSeries.getMaxX() / 1000) * 100) * 2)){
            data.minY += shiftOnScale;
            data.maxY -= shiftOnScale;
        }
    }

    private void scroll(float pan){
        shiftOnScale = (Math.round((data.maxY - data.minY) / 10000) * 100);

        if (pan > 10 && data.maxY < data.MAXY){
            data.minY += shiftOnScale;
            data.maxY += shiftOnScale;
            }

        if (pan < -10 && data.minY > data.MINY){
            data.minY -= shiftOnScale;
            data.maxY -= shiftOnScale;
            }
        }

    private void checkBoundaries(){
        data.heartPlot.setDomainBoundaries(data.minY, data.maxY, BoundaryMode.AUTO);
        }
    }